package modules

import (
	"service/components"
	"time"

	"gorm.io/gorm"
)

type Users struct {
	Id            int64          `json:"id" form:"id" gorm:"primaryKey;comment:id"`
	Rid           int64          `json:"rid" form:"rid" gorm:"comment:id"`
	NickName      string         `json:"nick_name" form:"nick_name" gorm:"type:varchar(300);comment:昵称"`
	UserName      string         `json:"user_name" form:"user_name" gorm:"type:varchar(300);comment:用户名字"`
	Password      string         `json:"password" form:"password" gorm:"type:varchar(300);comment:密码"`
	Sex           string         `json:"sex" form:"sex" gorm:"type:varchar(100);comment:性别"`
	Phone         string         `json:"phone" form:"phone" gorm:"type:varchar(20);comment:手机号码" valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email         string         `json:"email" form:"email" gorm:"type:varchar(300);comment:邮箱" valid:"email"`
	Description   string         `json:"description" form:"description" gorm:"type:text;comment:描述"`
	Department    string         `json:"department" form:"department" gorm:"type:text;comment:部门"`
	Permissions   string         `json:"permissions" form:"permissions" gorm:"type:text;comment:权限"`
	LoginIp       string         `json:"login_ip" form:"login_ip" gorm:"comment:登录的IP"`
	Avatar        string         `json:"avatar" gorm:"type:varchar(300);comment:头像"`
	Background    string         `json:"background" gorm:"type:varchar(300);comment:背景"`
	IsLogin       bool           `json:"is_login" gorm:"default:false;comment:登录标识"`
	ShowOrderInfo bool           `json:"show_order_info" gorm:"default:false;comment:显示订单信息"`
	Status        bool           `json:"status" form:"status" gorm:"comment:状态"`
	MailIsAllowed bool           `json:"mail_is_allowed" gorm:"comment:允许发送邮件"`
	CreatedAt     time.Time      `json:"created_at" form:"created_at" gorm:"comment:注册时间"`
	UpdatedAt     time.Time      `json:"updated_at" form:"updated_at" gorm:"comment:更新时间"`
	DeletedAt     gorm.DeletedAt `json:"deleted_at" form:"deleted_at" gorm:"comment:删除时间"`
	UpdateBalance bool           `json:"update_balance" gorm:"default:false;comment:更新库存"`
	ReadAccount   bool           `json:"read_account"  gorm:"default:false;comment:账号更新数据"`
	EditAccount   bool           `json:"edit_account"  gorm:"default:false;comment:是否编辑"`
	OrderPrefix   string         `json:"order_prefix" gorm:"comment:订单前缀"`
}

func (u *Users) TableName() string {
	return "users"
}

func CreateUserService(user *Users) error {
	user.Avatar = ""
	user.Background = ""
	err := components.DB.Model(user).Create(user).Error
	return err
}

func FindUserByNameService(name string) (*Users, error) {
	user := new(Users)
	err := components.DB.Where("user_name = ?", name).First(user).Error
	return user, err
}

func FindUserByIdOmitPadService(id int64) (*Users, error) {
	user := new(Users)
	err := components.DB.Where("id = ?", id).Omit("password").First(user).Error
	return user, err
}
func FindUserByPhoneService(phone string) (*Users, error) {
	user := new(Users)
	err := components.DB.Where("phone = ?", phone).First(user).Error
	return user, err
}
func FindUserByEmailService(email string) (*Users, error) {
	user := new(Users)
	err := components.DB.Where("email = ?", email).First(user).Error
	return user, err
}
func FindUserByOrderPrefixService(order_prefix string) (*Users, error) {
	user := new(Users)
	err := components.DB.Where("order_prefix = ?", order_prefix).First(user).Error
	return user, err
}
func FindAllUserService() ([]*Users, int64) {
	users := make([]*Users, 20)
	count := components.DB.Model(&Users{}).Omit("password").Find(&users).RowsAffected
	return users, count
}
func UpdatedUserInfoService(user Users) error {
	user.EditAccount = true
	err := components.DB.Model(&Users{}).
		Select("*").Omit("password").
		Where("id = ?", user.Id).
		Updates(user).Error
	return err
}
func DeleteUserService(id string) error {
	u := Users{}
	err := components.DB.Model(Users{}).Where("id = ?", id).First(&u).Delete(&u).Error
	return err
}

/*
// 创建的SQL表
CREATE TABLE if not EXISTS `users` (
 id int NOT NULL AUTO_INCREMENT  PRIMARY KEY  COMMENT '用户ID',
 rid int COMMENT '角色ID',
 nickname VARCHAR(100) not NULL COMMENT '名字 昵称',
 username VARCHAR(100) not null COMMENT '真实姓名',
 `password` VARCHAR(100) NOT NULL COMMENT '密码',
 description VARCHAR(100) COMMENT '描述',
 `status` VARCHAR(1) NOT null COMMENT '状态',
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
 deleted_at TIMESTAMP null DEFAULT NULL COMMENT "删除时间"
)
*/
